/**
 * 权限声明：
 * 源码可自由修改。
 */
package com.handy.plugin.data;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.util.FileTool;
import com.handy.webwork.config.HandyConfiguration;

/**
 * 配置文件存储类,单例模式
 * 
 * @author rocken.zeng@gmail.com
 * 
 */
public class ScriptCache {

	private ScriptCache(){}
	/**
	 * 获取实例.
	 * 
	 * @return
	 */
	public static synchronized ScriptCache getInstance() {
		if (log.isDebugEnabled()){
			log.debug("getInstance()");
		}
		if (instance == null) {
			instance = new ScriptCache();
		}
		return instance;
	}
	
	public static void putScript(ScriptBean bean){
		if (null!=instance.scripts.get(bean.getId())){
			instance.scripts.remove(bean.getId());
			log.info("警告：脚本id 重复定义 = " + bean.getId() );
		}
		instance.scripts.put(bean.getId(), bean);
	}
	
	public static ScriptBean findScript(String id){
		return instance.scripts.get(id);
	}

	@SuppressWarnings("unchecked")
	public static void loadScript() {
		//遍历classpath下所有_*.js文件
		for (String filePath : HandyConfiguration.scriptFiles()) {
			if (!filePath.startsWith("/")) filePath = "/" + filePath;
//			log.info(filePath);
			String fileContent = FileTool.readFileToString(ScriptCompile.class
					.getResourceAsStream(filePath),HandyConfiguration.properties(HandyConfiguration.PROP_ENCODING));
//			log.info(fileContent);
			ScriptBean bean = new ScriptBean();
			bean.setFilePath(filePath);
			bean.setFileContent(fileContent);
			ScriptCompile.scriptProperty(bean);//处理脚本，获取脚本编号与状态
			//满足条件的脚本才加入脚本集合
			if (bean.getId() != null && !"".equals(bean.getId()) && bean.getStatus()==1){
				ScriptCache.putScript(bean);
			}
		}
		//遍历web-inf下所有_*.js文件
		if (null != ScriptCache.getInstance().getExtendScriptFile()){
			loadOutScript();
		}
	}

	public static void loadOutScript() {
		List<String> listFilePath = FileTool.listFile(HandyConfiguration.rootPath() + ScriptCache.getInstance().getExtendScriptFile());
		for(String filePath : listFilePath){
			if (filePath.toUpperCase().endsWith(".JS")){//脚本文件
				//分析文件名
				String jsFilename = filePath.substring(filePath.replace("\\","/").lastIndexOf('/')+1);
				if (jsFilename.startsWith("_")){
					String fileContent = FileTool.readFileToString( filePath ,HandyConfiguration.properties(HandyConfiguration.PROP_ENCODING));
					ScriptBean bean = new ScriptBean();
					bean.setFilePath(filePath);
					bean.setFileContent(fileContent);
					ScriptCompile.scriptProperty(bean);//处理脚本，获取脚本编号与状态
					//满足条件的脚本才加入脚本集合
					if (bean.getId() != null && !"".equals(bean.getId()) && bean.getStatus()==1){
						ScriptCache.putScript(bean);
						log.info("add "+bean.getId());
						log.info(bean.getFilePath());
						log.info(bean.getFileContent());
					}
				}
			}
		}
	}
	
	
	private static Log log = LogFactory.getLog(ScriptCache.class);
	private static ScriptCache instance = null;
	
	private HashMap<String,ScriptBean> scripts = new HashMap<String,ScriptBean>();
	
	public static final String PROP_ISCACHE = "isCache";
	public static final String PROP_DBKIND = "dbKind";
	public static final String PROP_EXTENDSCRIPTFILE = "extendScriptFile";
	
	private Integer isCache = -1;
	
	private Integer dbKind = 1;
	
	private String extendScriptFile = "";

	public Integer getIsCache() {
		return isCache;
	}
	public void setIsCache(Integer isCache) {
		this.isCache = isCache;
	}
	public HashMap<String, ScriptBean> getScripts() {
		return scripts;
	}
	public void setScripts(HashMap<String, ScriptBean> scripts) {
		this.scripts = scripts;
	}
	public Integer getDbKind() {
		return dbKind;
	}
	public void setDbKind(Integer dbKind) {
		this.dbKind = dbKind;
	}
	public String getExtendScriptFile() {
		return extendScriptFile;
	}
	public void setExtendScriptFile(String extendScriptFile) {
		this.extendScriptFile = extendScriptFile;
	}

}